﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.BusinessRules.View;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;

namespace Basic4Net.OAWeb.Module_Report
{
    public partial class VacationWorkLoad : BasePage
    {
        public  List<TeachVacation> holidayList = new List<TeachVacation>();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropList();
                InitMonthList();
                Bind(0, aspNetPager.PageSize);
            } 
        }


        private void InitDropList()
        {
            List<t_UnSetWork> list = UnSetWork.UnSetWorkInstance.FindAll(SchoolMark);

            dropList1.DataValueField = "RID";
            dropList1.DataTextField = "WorkName";
            dropList1.DataSource = list;
            dropList1.DataBind();
        }

        private void InitMonthList()
        {
            Amount amountLogic = new Amount();
            IList<string> yearList = amountLogic.GetAmountMonth(SchoolMark, 1);
            if (yearList == null || yearList.Count < 1)
            {
                dropMonth.Items.Add(new ListItem("--当前没有假期工作量数据--", "-1"));
                return;
            }
            dropMonth.DataSource = yearList;
            dropMonth.DataBind();
        }


        protected void btnItemBind_Click(object sender, EventArgs e)
        {
            Bind((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }



        private void Bind(int startAt, int step)
        { 
            string monthValue = dropMonth.SelectedItem.Value.ToString();
            int month = 0, year = 0;
            if(!string.IsNullOrEmpty(monthValue))
            {
                if(monthValue.Split('-').Length > 0)
                {
                    string[] arr = monthValue.Split('-');
                    int.TryParse(arr[0], out year);
                    int.TryParse(arr[1], out month);
                }
            }
            int postId = dropList1.SelectedItem.Value == null ? -1 : Convert.ToInt32(dropList1.SelectedItem.Value);
            string teachCode = TeachCode.Text;
            int count = 0;

            List<string> teachIds = new List<string>();
            IList<TeachPostEntity> teachList = PostRelation.PostRelationInstance.GetTeachInfoByPost(0, 0, postId, null, SchoolMark, ref count);
            foreach (var teachPostEntity in teachList)
            {
                teachIds.Add(teachPostEntity.TeacherID.ToString()); 
            }

            holidayList = MeritPay.MeritPayInstance.GetHolidayData(startAt, step, SchoolMark, year, month, string.Join(",", teachIds.ToArray()), teachCode, ref count);
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = teachList;
            Repeater1.DataBind();
        }

        public string GetTdInfo(int teacherId)
        {
            List<TeachVacation> newList = holidayList.FindAll(delegate(TeachVacation t)
                                                                  { return t.TeacherID == teacherId; });
            string str = "<td>";
            str += newList[0].HolidayWork; 
            str = "</td>";
            str = "<td>";
            str += newList[0].HolidayWorkContent; 
            str = "</td>";
            return str;
        }



        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            aspNetPager.PageIndex = e.NewPageIndex;
            Bind((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }


        /// <summary>
        /// 职务分类下拉事件
        /// </summary>
        protected void DrPostChanged(object sender, EventArgs e)
        {
            Bind(0, aspNetPager.PageSize);
        } 
        
        
        /// <summary>
        /// 月份下拉事件
        /// </summary>
        protected void DrMonthChanged(object sender, EventArgs e)
        {
            Bind(0, aspNetPager.PageSize);
        }
    }

    public class MonthExpresson
    {
        public MonthExpresson()
        {
        }

        public MonthExpresson(int month,string monthName)
        {
            m_month = month;
            m_MonthName = monthName;
        }

        private int m_month;
        public int Month
        {
            get { return m_month; }
            set { m_month = value; }
        }


        private string m_MonthName;
        public string MonthName
        {
            get { return m_MonthName; }
            set { m_MonthName = value; }

        }

    }
}